
#include "e_mod_main.h"
#ifdef HAVE_LIBMPD_LIBMPD_H
#include <libmpd/libmpd.h>


void eloquence_mpd_init_menu		(E_Menu *menu, Eloquence_Face *face);
void eloquence_mpd_connect		(void *data);
void eloquence_mpd_disconnect		(void *data);
int  eloquence_mpd_update_song		(void *data);
void eloquence_mpd_control_play_pause	(void *data);
void eloquence_mpd_control_track_next	(void *data);
void eloquence_mpd_control_track_prev	(void *data);
void eloquence_mpd_control_vol_inc	(void *data);
void eloquence_mpd_control_vol_dec	(void *data);
void eloquence_mpd_control_shuffle	(void *data);
void eloquence_mpd_add_file_callback	(void *data,Evas_Object *obj, const char *emission, const char *source);

static int 		_mpd_connect		(void *data);
static const char 	*_mpd_get_text		(MpdObj *mo, char *buffer);
static char 		*_mpd_get_textVol	(MpdObj *mo, char *buffer, int volume);
static void 		_mpd_control_vol	(Eloquence_Face *face, int value);

void eloquence_mpd_init_menu(E_Menu *menu, Eloquence_Face *face)
{
   E_Menu_Item *mi;

   mi = e_menu_item_new(menu);
   e_menu_item_label_set(mi, _("Play/Pause"));
   e_menu_item_callback_set(mi, eloquence_mpd_control_play_pause, face);
   mi = e_menu_item_new(menu);
   e_menu_item_label_set(mi, _("Next Song"));
   e_menu_item_callback_set(mi, eloquence_mpd_control_track_next, face);
   mi = e_menu_item_new(menu);
   e_menu_item_label_set(mi, _("Previous Song"));
   e_menu_item_callback_set(mi, eloquence_mpd_control_track_prev, face);
   mi = e_menu_item_new(menu);
   e_menu_item_label_set(mi, _("Shuffle On/Off"));
   e_menu_item_callback_set(mi, eloquence_mpd_control_shuffle, face);
}

void eloquence_mpd_connect(void *data)
{
   Eloquence_Face *face;

   face = data;
   if ( _mpd_connect(face) != 0 )
     face->recheck_timer = ecore_timer_add(SEARCH_REFRESH, _mpd_connect, face);
}

void eloquence_mpd_disconnect(void *data)
{
   Eloquence_Face *face;

   face = data;
   if (face->session.ptr)
     {
	mpd_disconnect(face->session.ptr);
	face->session.ptr = NULL;
     }

   if (face->recheck_timer)
     {
	ecore_timer_del(face->recheck_timer);
	face->recheck_timer = NULL;
     }
}

int eloquence_mpd_update_song(void *data)
{
   Eloquence_Face *face;
   MpdObj *mo;

   face = data;
   mo = face->session.ptr;

   if (mo)
     {
	mpd_status_update(mo);
	face->song = _mpd_get_text(mo, face->outbuf);
	draw_text(face);
     }

   return 1;
}

void eloquence_mpd_control_play_pause(void *data)
{
   Eloquence_Face *face;

   face = data;
   mpd_sendStatusCommand(face->session.ptr);
   mpd_Status *status = mpd_getStatus(face->session.ptr);

   if (status)
     {
	if (status->state <= MPD_STATUS_STATE_STOP)
	  mpd_sendPlayCommand(face->session.ptr, -1); // -1 == beginning
	else
	  mpd_sendPauseCommand(face->session.ptr, status->state == MPD_STATUS_STATE_PLAY);

	mpd_freeStatus(status);
     }
}

void eloquence_mpd_control_track_next(void *data) 
{
   Eloquence_Face *face;

   face = data;
   mpd_sendNextCommand(face->session.ptr);
   eloquence_mpd_update_song(face);
}

void eloquence_mpd_control_track_prev(void *data) 
{
   Eloquence_Face *face;

   face = data;
   mpd_sendPrevCommand(face->session.ptr);
   eloquence_mpd_update_song(face);
}

void eloquence_mpd_control_vol_inc(void *data) 
{
   Eloquence_Face *face;

   face = data;
   _mpd_control_vol(face, 2);
}

void eloquence_mpd_control_vol_dec(void *data) 
{
   Eloquence_Face *face;

   face = data;
   _mpd_control_vol(face, -2);
}

void eloquence_mpd_control_shuffle(void *data) 
{
   Eloquence_Face *face;

   face = data;
   mpd_sendShuffleCommand(face->session.ptr);
}

void eloquence_mpd_add_file_callback(void *data, 
      Evas_Object *obj, const char *emission, const char *source)
{
   /* Empty now, for drag'n'drop of files to play */
}


static int _mpd_connect(void *data)
{
   Eloquence_Face *face;
   MpdObj *mo;

   face = data;
   mo = mpd_new_default();
   if (!mpd_connect(mo))
     {
	face->session.ptr = mo;
	return 0;
     }
   else
     {
	return 1;
     }
}

static const char *_mpd_get_text(MpdObj *mo , char *buffer)
{
   buffer[0] = '\0';
   strcat(buffer, "code being rewritten ...");

   /* TODO : reimplement the function
      mpd_Status *status = mpd_getStatus(mpd);

      if (status)
      {
      if (status->state > MPD_STATUS_STATE_STOP)
      {
      mpd_sendCurrentSongCommand(mpd);
      mpd_InfoEntity *entity = NULL;

      while ( (entity = mpd_getNextInfoEntity(mpd)) )
      {
      if ( entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
      entity->info.song->id == status->songid )
      {
      mpd_Song *song = entity->info.song;

      if (song->artist)
      strcpy(buffer, song->artist);
      if (song->title)
      {
      if (buffer[0] != '\0')
      strcat(buffer, " - ");
      strcat(buffer, song->title);
      }

   // no artist or title, use filename
   if (buffer[0] == '\0')
   strcpy(buffer, song->file);

   if (status->state == MPD_STATUS_STATE_PAUSE)
   strcat(buffer, " (paused)");
   }

   mpd_freeInfoEntity(entity);
   }
   }

   mpd_freeStatus(status);
   }*/

   return buffer;
}

static char *_mpd_get_textVol(MpdObj *mo, char *buffer, int volume)
{
   sprintf(buffer, "%s	 --	 Volume: %i%%", _mpd_get_text(mo, buffer), volume);
   return buffer;
}

static void _mpd_control_vol(Eloquence_Face *face, int value)
{
   mpd_sendStatusCommand(face->session.ptr);
   mpd_Status *status = mpd_getStatus(face->session.ptr);

   if (status)
     {
	if (status->volume != MPD_STATUS_NO_VOLUME)
	  {
	     int volume = status->volume + value;	

	     if (volume < 0)
	       volume = 0;
	     else if (volume > 100)
	       volume = 100;

	     face->song = _mpd_get_textVol(face->session.ptr, face->outbuf, volume);
	     draw_text(face);

	     mpd_sendSetvolCommand(face->session.ptr, volume);
	  }

	mpd_freeStatus(status);
     }
}


#endif // HAVE_LIBMPD_LIBMPD_H
